/**
 * 
 */
package com.et114.components.acegi.vote;

import java.util.Iterator;

import org.acegisecurity.Authentication;
import org.acegisecurity.ConfigAttribute;
import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache;
import org.acegisecurity.vote.AccessDecisionVoter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;

import com.et114.components.acegi.Constants;

/**
 * @author guanhw
 */
public class TitleVoter implements AccessDecisionVoter {
	private Log						logger		= LogFactory.getLog ( this.getClass ( ) );
	private String					titlePrefix	= Constants.VOTER_TITLE_PREFIX;
	private EhCacheBasedUserCache	userCache;
	private boolean					withCache	= true;
	
	public TitleVoter ( ) {
		// TODO Auto-generated constructor stub
	}

	public void setUserCache ( EhCacheBasedUserCache userCache ) {
		this.userCache = userCache;
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.vote.AccessDecisionVoter#supports(org.acegisecurity.ConfigAttribute)
	 */
	public boolean supports ( ConfigAttribute attribute ) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		if ( ( attribute.getAttribute() != null )
				&& attribute.getAttribute().startsWith( getTitlePrefix ( ) ) ) {
			return true;
		} else {
			return false;
		}

	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.vote.AccessDecisionVoter#supports(java.lang.Class)
	 */
	public boolean supports ( Class clazz ) {
		// TODO Auto-generated method stub
		return true;
	}

	/* (non-Javadoc)
	 * @see org.acegisecurity.vote.AccessDecisionVoter#vote(org.acegisecurity.Authentication, java.lang.Object, org.acegisecurity.ConfigAttributeDefinition)
	 */
	public int vote ( Authentication authentication , Object object ,
			ConfigAttributeDefinition config ) {
		// TODO Auto-generated method stub
		logger.debug(  "进入title 岗位投票器！！！"  ) ;
		int result = ACCESS_ABSTAIN;
		//if ( userCache.getUserFromCache ( authentication.getName ( ) ) == null ) return ACCESS_ABSTAIN ;
		
		GrantedAuthority[] userGas = new GrantedAuthority[ 0 ]  ; 
		if ( withCache ) {
			if ( userCache.getUserFromCache ( authentication.getName ( ) ) == null ) return ACCESS_ABSTAIN ;
			userGas = userCache.getUserFromCache ( authentication.getName ( ) ).getAuthorities ( ) ;
		}
		else {
			userGas = authentication.getAuthorities ( ) ;				
		}		
		if ( userGas.length == 0  ) {
			userGas = new GrantedAuthority [ 1 ];
			userGas [ 0 ] = new GrantedAuthorityImpl (
					Constants.VOTER_USER_PREFIX );				
		}		
		
		//GrantedAuthority[] userGas = userCache.getUserFromCache ( authentication.getName ( ) ).getAuthorities ( ) ; 
		for (int i = 0; i < userGas.length; i++) {
			logger.debug( "当前用户" + authentication.getName() + "..............." + userGas [ i ].getAuthority() ) ;
		}
		
		Iterator iter = config.getConfigAttributes();
		while (iter.hasNext()) {
			ConfigAttribute attribute = (ConfigAttribute) iter.next();

			if ( this.supports ( attribute ) ) {
				result = ACCESS_DENIED;
				// Attempt to find a matching granted authority
				for ( int i = 0 ; i < userGas.length ; i ++ ) {
					if (attribute.getAttribute().equals( userGas[i].getAuthority() ) ) {
						return ACCESS_GRANTED;
					}
				}
			}
		}
		logger.debug(  "当前岗位：" +  authentication.getName()  + " 投票结果： " + result   ) ; 
		return result;
	}

	public String getTitlePrefix ( ) {
		return titlePrefix;
	}

	public void setTitlePrefix ( String titlePrefix ) {
		Assert.notNull( titlePrefix, "titlePrefix must not be null" );
		Constants.VOTER_TITLE_PREFIX = titlePrefix ;
		this.titlePrefix = titlePrefix;
	}

	public boolean getWithCache ( ) {
		return withCache;
	}

	public void setWithCache ( boolean withCache ) {
		this.withCache = withCache;
	}

}
